package com.dw.widget.slideshow.config.view.list;

import java.util.EventListener;

import com.dw.widget.slideshow.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ListCheckItemView extends BaseListItemView {

	private ListItemIconView iconView;
	private RelativeLayout rlSelected;
	private CheckBox cbSelected;
	private TextView tvText;
	
	private OnCheckBoxSwitchListener cbListener;
	private OnListItemClickListener clickListener;
	
	public ListCheckItemView(Context context, AttributeSet attrs, int defStyle) {
		
		super(context, attrs, defStyle);
	}

	public ListCheckItemView(Context context, AttributeSet attrs) {
		this(context, attrs, -1);
	}
	
	public ListCheckItemView(Context context) {
		
		this(context, null);
	}

	@Override
	protected void onFinishInflate() {

		super.onFinishInflate();
		iconView = (ListItemIconView)findViewById(R.id.listitemcheck_icon);
		cbSelected = (CheckBox)findViewById(R.id.listitemcheck_cb);
		rlSelected = (RelativeLayout)findViewById(R.id.listitemcheck_cbll);
		tvText = (TextView)findViewById(R.id.listitemcheck_text);
		
		// 整行选择事件
		cbSelected.setFocusable(false);
		cbSelected.setClickable(false);
		
		rlSelected.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				boolean isChecked = !cbSelected.isChecked();
				cbSelected.setChecked(isChecked);
				
				if(cbListener != null)
				{
					cbListener.OnSwitch(isChecked);
				}
			}
		});
		
		// 点击事件
		iconView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				if(clickListener != null)
				{
					clickListener.OnClick(ListCheckItemView.this);
				}
			}
		});
		tvText.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				if(clickListener != null)
				{
					clickListener.OnClick(ListCheckItemView.this);
				}
			}
		});
	}
	
	public void setText(String text)
	{
		if(text == null)
		{
			return;
		}
		
		tvText.setText(text);
	}
	
	public void setIcon(int icon)
	{
		if(icon < 0 )
		{
			return;
		}
		
		iconView.setIcon(icon);
	}
	
	public void setChecked(boolean checked)
	{
		cbSelected.setChecked(checked);
	}
	
	public boolean isSelected()
	{
		return cbSelected.isChecked();
	}
	
	public void setOnCheckBoxSwitchListener(OnCheckBoxSwitchListener cbListener)
	{
		this.cbListener = cbListener;
	}
	
	public void setOnListItemClickListener(OnListItemClickListener listener)
	{
		this.clickListener = listener;
	}
	
	public interface OnCheckBoxSwitchListener extends EventListener
	{
		abstract public void OnSwitch(boolean on);
	}
	
	public interface OnListItemClickListener extends EventListener
	{
		abstract public void OnClick(ListCheckItemView view);
	}
}
